// $Id$
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Xml.Serialization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataRules;
#if (!NET_CF)
using System.Diagnostics.CodeAnalysis;
#endif

namespace Northwind.DomainModel
{

    /// <summary />
    partial interface IProductContainer
    {
        /// <summary />
      Product Product
        {
            get;
            set;
        }

    }

    /// <summary />
    partial interface IProductMapper
    {

        /// <summary />
        Product Find(Product.PrimaryKey key, DataMapperCollection dataMappers);
        /// <summary />
        DomainModelKeyedCollection<Product> FindAll(DataMapperCollection dataMappers);

    }

    /// <summary />
    partial class Product
    {

        /// <summary />
        partial class PrimaryKey
        {
            /// <summary />
            public static implicit operator PrimaryKey(int key)
            {
                return new PrimaryKey(key);
            }

            /// <summary />
            public static explicit operator int(PrimaryKey key)
            {
                return key.Value;
            }

        }

        /// <summary />
        partial class DataTransfer
        {
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int ProductID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int? CategoryID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int? SupplierID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string ProductName;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string QuantityPerUnit;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public decimal? UnitPrice;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public short? UnitsInStock;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public short? UnitsOnOrder;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public short? ReorderLevel;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public bool? Discontinued;

            /// <summary />
            public object Clone()
            {
                return MemberwiseClone();
            }

        }

        /// <summary />
        protected override Product.PrimaryKey CreateId()
        {
            return new Product.PrimaryKey(Data.ProductID);
        }

        /// <summary />
        internal Product.PrimaryKey GetId()
        {
            return Id;
        }

        /// <summary />
        internal void SetId(int id)
        {
            Data.ProductID=id;
        }


        /// <summary />
#if (!NET_CF)
        [DataObjectMethod(DataObjectMethodType.Select, false)]
#endif
        public static Product Find(Product.PrimaryKey key, DataMapperCollection dataMappers)
        {
            return ((IProductMapper)dataMappers[typeof(Product)]).Find(key, dataMappers);
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectMethod(DataObjectMethodType.Select, true)]
#endif
        public static IList<Product> FindAll(DataMapperCollection dataMappers)
        {
            return ((IProductMapper)dataMappers[typeof(Product)]).FindAll(dataMappers);
        }

        /// <summary />
        private void _LoadCategory(bool lazyload)
        {
            if (IsNew)
                return;

            if ((_Category==null) && (Data.CategoryID.HasValue))
                _Category=((ICategoryMapper)DataMappers[typeof(Category)]).Find(Data.CategoryID, DataMappers);
        }

        /// <summary />
        private void _Category_Saved(object sender, PersistenceEventArgs e)
        {
            int? id=((Category)sender).GetId().Value;
            if (Data.CategoryID!=id)
            {
                Data.CategoryID=id;
                IsModified=true;
            }

            ((Category)sender).Saved-=_Category_Saved;
        }

        /// <summary />
        private void _LoadSupplier(bool lazyload)
        {
            if (IsNew)
                return;

            if ((_Supplier==null) && (Data.SupplierID.HasValue))
                _Supplier=((ISupplierMapper)DataMappers[typeof(Supplier)]).Find(Data.SupplierID, DataMappers);
        }

        /// <summary />
        private void _Supplier_Saved(object sender, PersistenceEventArgs e)
        {
            int? id=((Supplier)sender).GetId().Value;
            if (Data.SupplierID!=id)
            {
                Data.SupplierID=id;
                IsModified=true;
            }

            ((Supplier)sender).Saved-=_Supplier_Saved;
        }

        /// <summary />
        internal bool InternalIsNew
        {
            get
            {
                return IsNew;
            }
        }

        /// <summary />
        private Category _CategoryProperty
        {
            get
            {
                Load(true);
                return _Category;
            }
            set
            {
                _LoadCategory(true);
                if (_Category!=value)
                {
                    if ((_Category!=null) && (_Category.InternalIsNew))
                        _Category.Saved-=_Category_Saved;
                    _Category=value;
                    if (value!=null)
                    {
                        if (_Category.InternalIsNew)
                            _Category.Saved+=_Category_Saved;
                        else
                            Data.CategoryID=(int?)value.GetId();
                    } else
                      Data.CategoryID=null;
                }
            }
        }

        /// <summary />
        private Supplier _SupplierProperty
        {
            get
            {
                Load(true);
                return _Supplier;
            }
            set
            {
                _LoadSupplier(true);
                if (_Supplier!=value)
                {
                    if ((_Supplier!=null) && (_Supplier.InternalIsNew))
                        _Supplier.Saved-=_Supplier_Saved;
                    _Supplier=value;
                    if (value!=null)
                    {
                        if (_Supplier.InternalIsNew)
                            _Supplier.Saved+=_Supplier_Saved;
                        else
                            Data.SupplierID=(int?)value.GetId();
                    } else
                      Data.SupplierID=null;
                }
            }
        }

        /// <summary />
        private string _ProductNameProperty
        {
            get
            {
                Load(true);
                return Data.ProductName;
            }
            set
            {
                if (Data.ProductName!=value)
                {
                    Data.ProductName=value;
                }
            }
        }

        /// <summary />
        private string _QuantityPerUnitProperty
        {
            get
            {
                Load(true);
                return Data.QuantityPerUnit;
            }
            set
            {
                if (Data.QuantityPerUnit!=value)
                {
                    Data.QuantityPerUnit=value;
                }
            }
        }

        /// <summary />
        private decimal? _UnitPriceProperty
        {
            get
            {
                Load(true);
                return Data.UnitPrice;
            }
            set
            {
                if (Data.UnitPrice!=value)
                {
                    Data.UnitPrice=value;
                }
            }
        }

        /// <summary />
        private short? _UnitsInStockProperty
        {
            get
            {
                Load(true);
                return Data.UnitsInStock;
            }
            set
            {
                if (Data.UnitsInStock!=value)
                {
                    Data.UnitsInStock=value;
                }
            }
        }

        /// <summary />
        private short? _UnitsOnOrderProperty
        {
            get
            {
                Load(true);
                return Data.UnitsOnOrder;
            }
            set
            {
                if (Data.UnitsOnOrder!=value)
                {
                    Data.UnitsOnOrder=value;
                }
            }
        }

        /// <summary />
        private short? _ReorderLevelProperty
        {
            get
            {
                Load(true);
                return Data.ReorderLevel;
            }
            set
            {
                if (Data.ReorderLevel!=value)
                {
                    Data.ReorderLevel=value;
                }
            }
        }

        /// <summary />
        private bool? _DiscontinuedProperty
        {
            get
            {
                Load(true);
                return Data.Discontinued;
            }
            set
            {
                if (Data.Discontinued!=value)
                {
                    Data.Discontinued=value;
                }
            }
        }

        /// <summary />
        private Category _Category;
        /// <summary />
        private Supplier _Supplier;

        /// <summary />
        public static readonly IdentityMap<Product.PrimaryKey, Product> IdentityMap=new IdentityMap<Product.PrimaryKey, Product>();

    }

}
